package com.runshu.newsmartark.mvp.presenter;

import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.runshu.newsmartark.R;
import com.runshu.newsmartark.activity.MainActivity;
import com.runshu.newsmartark.base.MyApplication;
import com.runshu.newsmartark.common.ConstantUrl;
import com.runshu.newsmartark.dbutil.DBUtils;
import com.runshu.newsmartark.dbutil.ProAllEntity;
import com.runshu.newsmartark.dbutil.ProSelfEntity;
import com.runshu.newsmartark.dbutil.RefreshUtils;
import com.runshu.newsmartark.entity.ConfirmStockEntity;
import com.runshu.newsmartark.entity.NewTakeStockEntity;
import com.runshu.newsmartark.entity.ProductsBean;
import com.runshu.newsmartark.event.Event;
import com.runshu.newsmartark.event.EventBusUtils;
import com.runshu.newsmartark.event.EventCode;
import com.runshu.newsmartark.mvp.contract.ConfirmStockContract;
import com.runshu.newsmartark.mvp.contract.ProductsByCategoryContract;
import com.runshu.newsmartark.network.netbean.ResponseBean;
import com.runshu.newsmartark.network.netinterface.MyCallBack;
import com.runshu.newsmartark.util.RefUtil;
import com.runshu.newsmartark.util.SoundPoolUtils;
import com.runshu.newsmartark.util.common.SharedPreferencesUtils;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

/**
 * Demo class
 *
 * @author yu
 * @date 2020/10/28·
 * @decs：
 */

public class ConfirmStockPresenter implements ConfirmStockContract.Presenter {
    private ConfirmStockContract.View view;
    private ConfirmStockContract.Model model;

    public ConfirmStockPresenter(ConfirmStockContract.View view, ConfirmStockContract.Model model) {
        this.view = view;
        this.model = model;
    }


    @Override
    public void getConfirmStock(String macAddress, String userCode, List<String> refIds, String type) {
        OwnTask task = new OwnTask( view );
        task.execute( refIds );
    }

    @Override
    public void getGoodsBySerno(List<String> refIds) {
        model.getGoodsBySerno( refIds, new MyCallBack() {
            @Override
            public void onSuccess(ResponseBean data) {
                new Thread( new Runnable() {
                    @Override
                    public void run() {
                        List<ProAllEntity> allList = (List<ProAllEntity>) data.pullData();
                        if (allList != null) {
                            for (int i = 0; i < allList.size(); i++) {
                                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                allList.get(i).setUpdateTime(dateFormat.format(new Date(0)));
                            }

                            DBUtils.getInstance().insertAllProList( allList );
                        }
                        SharedPreferencesUtils.put( MyApplication.getApplication(), ConstantUrl.Share_LoadingFirst, false );
                        //刷新医院商品
                        List<String> serialNo = DBUtils.getInstance().getProAllDao().getSerialNo();
                        if (allList != null ) {
                            List<ProSelfEntity> proBySerialNos = DBUtils.getInstance().getProBySerialNosToSelfEntity( serialNo.toArray( new String[serialNo.size()] ) );
                            DBUtils.getInstance().getProAllDao().insertSelfPro( proBySerialNos );
                        }
                        EventBusUtils.sendEvent( new Event( EventCode.EventRefreshGoodsInfo ) );
                    }
                } ).start();
            }

            @Override
            public void onError(ResponseBean data) {

            }
        } );
    }

    @Override
    public void checkInventory(String customerId, String hospitalId, String hwMac, List<String> rfidsList, int afreshType) {
        model.checkInventory(customerId, hospitalId, hwMac, rfidsList, afreshType,new MyCallBack() {
            @Override
            public void onSuccess(ResponseBean data) {
                view.onCheckInventorySuccess(data);
            }

            @Override
            public void onError(ResponseBean data) {
                view.onError(data.getMessage());
            }
        });
    }

    static class OwnTask extends AsyncTask<List<String>, Integer, ConfirmStockEntity> {
        private ConfirmStockContract.View view;

        public OwnTask(ConfirmStockContract.View view) {
            this.view = view;
        }

        @Override
        protected ConfirmStockEntity doInBackground(List<String>... lists) {
            List<String> list = new ArrayList<>();
            for (String tmp : lists[0]) {
                if (TextUtils.isEmpty( tmp )) continue;
                String e = RefUtil.decryptRfid( tmp );
                if (TextUtils.isEmpty( e )) continue;
                list.add( e );
            }
//            System.out.println(lists);

            ConfirmStockEntity result = RefreshUtils.getInstance().getGoodsExchange( list.toArray( new String[list.size()] ) );
            return result;
        }

        @Override
        protected void onPostExecute(ConfirmStockEntity confirmStockEntity) {
            super.onPostExecute( confirmStockEntity );
            if (confirmStockEntity == null) return;
            ResponseBean responseBean = new ResponseBean();
            responseBean.setData( new Gson().toJson( confirmStockEntity ) );
            responseBean.setCode( "200" );
            responseBean.setMessage( "成功" );
            responseBean.setClass( ConfirmStockEntity.class );
            view.OnGetConfirmStockSuccess( responseBean );
        }
    }
}